package com.xidian.zhiancloud.service.riskcontrol.riskidenva;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xidian.zhiancloud.dao.riskcontrol.riskidenva.TRiskCheckProcessMapper;
import com.xidian.zhiancloud.dao.riskcontrol.riskidenva.TRiskIdentEvalStatusMapper;
import com.xidian.zhiancloud.dao.riskcontrol.riskidenva.TRiskProcessOperationRecordMapper;
import com.xidian.zhiancloud.dao.riskcontrol.riskidenva.TRiskTaskAllocationMapper;
import com.xidian.zhiancloud.model.riskcontrol.TRiskCheckProcessList;
import com.xidian.zhiancloud.model.riskcontrol.TRiskProcessOperationRecordList;
import com.xidian.zhiancloud.model.riskcontrol.TRiskTaskAllocationList;
import com.xidian.zhiancloud.qo.BaseReqQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 风险辨识评价计划-分析评价任务分配
 */
@Service
public class TRiskTaskAllocationService extends ServiceImpl
        <TRiskTaskAllocationMapper, TRiskTaskAllocationList> {

    @Autowired
    TRiskIdentEvalStatusMapper tRiskIdentEvalStatusMapper;
    
    @Autowired
    TRiskTaskAllocationMapper tRiskTaskAllocationMapper;

    @Autowired
    HttpServletRequest request;

    @Autowired
    TRiskCheckProcessMapper tRiskCheckProcessMapper;

    @Autowired
    TRiskProcessOperationRecordMapper tRiskProcessOperationRecordMapper;

    public Page<TRiskTaskAllocationList> getTRiskTaskAllocation
            (BaseReqQuery reqdata, Long companyId,
             String triskidentevalsId,String status){
        PageHelper.startPage(reqdata.getPageNum(), reqdata.getPageSize());
        return tRiskTaskAllocationMapper.getTRiskTaskAllocation
                (reqdata.getSearchKey(),companyId,triskidentevalsId,status);
    }

    public List<TRiskTaskAllocationList> getCompanyIdAndPersonidenEva
            (Long companyId,String triskidentevalsId,String state,
             String personidenEva){
       return tRiskTaskAllocationMapper.getCompanyIdAndPersonidenEva
               (companyId,triskidentevalsId,state,personidenEva);
    }

    public List<TRiskTaskAllocationList> getCompanyIdAndDiscerner
            (Long companyId,String triskidentevalsId,String state,String discerner){
        return tRiskTaskAllocationMapper.getCompanyIdAndDiscerner
                (companyId,triskidentevalsId,state,discerner);
    }

    public List<TRiskTaskAllocationList> getCompanyIdAndEvaluator
            (Long companyId,String triskidentevalsId,String state,String evaluator){
        return tRiskTaskAllocationMapper.getCompanyIdAndEvaluator
                (companyId,triskidentevalsId,state,evaluator);
    }


    //分析评价任务分配-删除按钮
    public int removeTaskAllocation(String id){
        List<String> list = getList(id);
        return tRiskTaskAllocationMapper.removeTaskAllocation(list);
    }

    //和风险辨识评价计划一并-删除
    public int removeTRiskTaskAll(Long companyId,String id,String taskAllocatId){
        List<String> list = getList(taskAllocatId);
        int res = tRiskTaskAllocationMapper.removeTRiskTaskAllocation(list);
        if (res!=0){
            tRiskProcessOperationRecordMapper.deleteByTaskAllId(companyId, id, taskAllocatId);
        }
        return res; 
    }

    //和风险辨识评价计划一并-删除
    public int removeTRiskTaskAllocation(String taskAllocatId){
        List<String> list = getList(taskAllocatId);
        int res = tRiskTaskAllocationMapper.removeTRiskTaskAllocation(list);
        return res;
    }

    /**
     * id放入list
     * @param id id(多个已逗号分隔)
     * @return List集合
     */
    public List<String> getList(String id) {
        List<String> list = new ArrayList<String>();
        String[] str = id.split(",");
        for (int i = 0; i < str.length; i++) {
            list.add(str[i]);
        }
        return list;
    }

    public void insertTRiskTaskAllocation
            (List<TRiskTaskAllocationList> tRiskTaskAllList){
        tRiskTaskAllocationMapper.insertTRiskTaskAllocationList(tRiskTaskAllList);
    }

    public int updateEvaluationMethodIds(String taskAllocatId,String evaluationMethodId){
        List<TRiskTaskAllocationList> list =
                getEvaluationMethodIds(taskAllocatId,evaluationMethodId);
        return tRiskTaskAllocationMapper.updateEvaluationMethodIds(list);
    }
    
    public int removeByTriskidentevalsId(Long companyId,String triskidentevalsId){
        return tRiskTaskAllocationMapper.removeByTriskidentevalsId(companyId,triskidentevalsId);
    }

    public List<TRiskTaskAllocationList> getEvaluationMethodIds(String taskAllocatId
            ,String evaluationMethodIds){
        List<TRiskTaskAllocationList> EvaluationMethodIdsList = new ArrayList<>();
        String[] evaluationMethodId = evaluationMethodIds.split(",");
        String[] id = taskAllocatId.split(",");
        for (int i = 0; i < evaluationMethodId.length; i++) {
            TRiskTaskAllocationList taskall = new TRiskTaskAllocationList();
            taskall.setEvaluationMethodId(evaluationMethodId[i]);
            taskall.setId(Long.valueOf(id[i]));
            EvaluationMethodIdsList.add(taskall);
        }
        return EvaluationMethodIdsList;
    }

    public TRiskTaskAllocationList getByAllAndId(Long companyId,String id){
        return tRiskTaskAllocationMapper.getByAllAndId(companyId,id);
    }
    
    public List<TRiskTaskAllocationList> getById(Long companyId,String id){
        return tRiskTaskAllocationMapper.getById(companyId,id);
    }

    public List<TRiskTaskAllocationList> getByIds
        (Long companyId,String taskAllocatId,String type){
        return tRiskTaskAllocationMapper.getByIds(companyId,taskAllocatId,type);
    }

    public Page<TRiskTaskAllocationList> shreddedDataPage
        (BaseReqQuery reqdata,Long companyId,String id,Long userId){
        PageHelper.startPage(reqdata.getPageNum(), reqdata.getPageSize());
        return tRiskTaskAllocationMapper.shreddedDataPage(reqdata.getSearchKey(),companyId,id,userId);
    }

    public List<TRiskTaskAllocationList> selectByMoreId
        (Long companyId,String id){
        List<String> list = getList(id);
        return tRiskTaskAllocationMapper.selectByMoreId(companyId,list);
    }

    public List<TRiskTaskAllocationList> selectById
            (Long companyId,String id,String states){
        List<String> list = getList(id);
        return tRiskTaskAllocationMapper.selectById(companyId,list,states);
    }

    //风险辨识评价计划-风险评价-风险点评价退回 //风险点评价中 退回后状态辨识中
    public void sendBackTaskAllReturnReason(
        Long companyId,String id,String state,String returnReason,Long userId){
        TRiskTaskAllocationList all=tRiskTaskAllocationMapper.
            selectByTaskAllId(companyId,id);
        String trideevaId=all.getTriskidentevalsId().toString();
        int result = tRiskTaskAllocationMapper.sendBackTaskAllReturnReason
            (id,state,returnReason);
        if(result>0){
            int riIdEvaResu = tRiskIdentEvalStatusMapper.evaluationDecomposeRiskIdenEva
                (companyId,trideevaId,state,"0");
            if(riIdEvaResu>0) {
                tRiskProcessOperationRecordMapper.updateByIdAndAppraiseNoPass(companyId,trideevaId,id);
            }
        }
        
    }
    
    //根据分解人进行查询
    public List<TRiskTaskAllocationList> getByPersonidenevaId(Long companyId,String userId){
       return tRiskTaskAllocationMapper.getByPersonidenevaId(companyId,userId);
    }

    public int updateResolveById(String id,List<String> idList){
        int res = tRiskTaskAllocationMapper.updateResolveById(idList);
        if (res != 0 ) {
            tRiskProcessOperationRecordMapper.updateResolveById(id,idList);
        }
        return res;
    }
    
    //根据辨识人进行查询
    public List<TRiskTaskAllocationList> getByDiscernerId(Long companyId,String userId){
        return tRiskTaskAllocationMapper.getByDiscernerId(companyId,userId);
    }

    public int updateByRecognizeId(String id,List<String> idList){
        int res = tRiskTaskAllocationMapper.updateByRecognizeId(idList);
        if (res != 0 ) {
            tRiskProcessOperationRecordMapper.updateByRecognizeId(id,idList);
        }
        return res;
    }

    public int updateByDepaAuditId(String id,List<String> idList){
        int res = tRiskTaskAllocationMapper.updateByDepaAuditId(idList);
        if (res != 0 ) {
            tRiskProcessOperationRecordMapper.updateByDepaAuditId(id,idList);
        }
        return res;
    }
    
    //根据评价人进行查询
    public List<TRiskTaskAllocationList> getByEvaluatorId(Long companyId,String userId){
        return tRiskTaskAllocationMapper.getByEvaluatorId(companyId,userId);
    }

    public int updateByAppraiseId(String id,List<String> idList){
        int res = tRiskTaskAllocationMapper.updateByAppraiseId(idList);
        if (res != 0 ) {
            tRiskProcessOperationRecordMapper.updateByAppraiseId(id,idList);
        }
        return res;
    }
    
    //风险辨识评价计划-分解保存 id 计划任务分解表单中的ID
    public int insertDiscernerAndEvaluator(Long companyId,
            String id,String discerner, String evaluator,Long userId){
        List<String> lists = getList(id);
        List<TRiskTaskAllocationList> tAll = tRiskTaskAllocationMapper.selectByMoreId(companyId,lists);
        List<TRiskTaskAllocationList> list =
                getDiscernerAndEvaluator(id,discerner,evaluator);
        int res = tRiskTaskAllocationMapper.insertDiscernerAndEvaluator(list);

        int triskidentevalsId = tAll.get(0).getTriskidentevalsId();
        
        //辨识人
        List<TRiskProcessOperationRecordList> recL=tRiskProcessOperationRecordMapper
            .getByRecognizeAndTAllIdList(companyId,lists);
        List<TRiskProcessOperationRecordList> recList = new ArrayList<>();

        String[] discerners = discerner.split(",");
        for (int i = 0; i < recL.size(); i++) {
            TRiskProcessOperationRecordList rec = new TRiskProcessOperationRecordList();
            rec.setId(recL.get(i).getId());
            rec.setOperator(Integer.valueOf(discerners[i]));
            recList.add(rec);
        }
        
        //评价人
        List<TRiskProcessOperationRecordList> appL=tRiskProcessOperationRecordMapper
            .getByAppraiseAndTAllIdList(companyId,lists);
        String[] evaluators = evaluator.split(",");
        List<TRiskProcessOperationRecordList> appList = new ArrayList<>();
        for (int i = 0; i < appL.size(); i++) {
            TRiskProcessOperationRecordList app = new TRiskProcessOperationRecordList();
            app.setId(recL.get(i).getId());
            app.setOperator(Integer.valueOf(evaluators[i]));
            appList.add(app);
        }
        if (res!=0) {
            
            //配置辨识人
            tRiskProcessOperationRecordMapper.updateRecognizeAndAppraise(recList);
            
            //配置评价人
            tRiskProcessOperationRecordMapper.updateAppraiseAndAppraise(appList);
            
        }
        
        return res;
    }


    //分解保存(保存辨识人和评价人)
    public List<TRiskTaskAllocationList> getDiscernerAndEvaluator(String ids
            ,String discerners,String evaluators){
        List<TRiskTaskAllocationList> taskallList = new ArrayList<>();
        String[] discerner = discerners.split(",");
        String[] evaluator = evaluators.split(",");
        String[] id = ids.split(",");
        for (int i = 0; i < discerner.length; i++) {
            TRiskTaskAllocationList taskall = new TRiskTaskAllocationList();
            taskall.setDiscerner(discerner[i]);
            taskall.setEvaluator(evaluator[i]);
            taskall.setId(Long.valueOf(id[i]));
            taskallList.add(taskall);
        }
        return taskallList;
    }

    //分解保存(保存辨识人和评价人)
    public List<TRiskCheckProcessList> getByCheckProcAndDiscernerAndEvaluator
        (String ids , String discerners, String evaluators){
        List<TRiskCheckProcessList> taskallList = new ArrayList<>();
        String[] discerner = discerners.split(",");
        String[] evaluator = evaluators.split(",");
        String[] id = ids.split(",");
        for (int i = 0; i < discerner.length; i++) {
            TRiskCheckProcessList checkProc = new TRiskCheckProcessList();
            checkProc.setDiscerner(discerner[i]);
            checkProc.setEvaluator(evaluator[i]);
            checkProc.setId(Long.valueOf(id[i]));
            taskallList.add(checkProc);
        }
        return taskallList;
    }

    public Page<TRiskTaskAllocationList> getCompanyIdAndPageTaskAll
            (BaseReqQuery reqdata,Long companyId,String id,
            Long userId,String type){
        PageHelper.startPage(reqdata.getPageNum(), reqdata.getPageSize());
        Page<TRiskTaskAllocationList> pagedata = tRiskTaskAllocationMapper.getCompanyIdAndPageTaskAll
                (reqdata.getSearchKey(),companyId,id,userId,type);
        return pagedata;
    }

    public Page<TRiskTaskAllocationList> getCompanyIdAndPersonidenEva
            (BaseReqQuery reqdata,Long companyId,String triskidentevalsId,
             String state,String personidenEva){
        PageHelper.startPage(reqdata.getPageNum(), reqdata.getPageSize());
        return tRiskTaskAllocationMapper.getCompanyIdAndPersonidenEva
           (reqdata.getSearchKey(),companyId,triskidentevalsId,state,personidenEva);
    }

    public Page<TRiskTaskAllocationList> getCompanyIdAndDiscerner
            (BaseReqQuery reqdata,Long companyId,String triskidentevalsId,
             String state,String discerner){

        PageHelper.startPage(reqdata.getPageNum(), reqdata.getPageSize());
        return tRiskTaskAllocationMapper.getCompanyIdAndDiscerner
                (reqdata.getSearchKey(),companyId,triskidentevalsId,state,discerner);

    }

    public Page<TRiskTaskAllocationList> getCompanyIdAndEvaluator
            (BaseReqQuery reqdata,Long companyId,String triskidentevalsId,
             String state,String evaluator){
        PageHelper.startPage(reqdata.getPageNum(), reqdata.getPageSize());
        return tRiskTaskAllocationMapper.getCompanyIdAndEvaluator
                (reqdata.getSearchKey(),companyId,triskidentevalsId,state,evaluator);
    }

    //风险点分解保存后TaskAll中State修改为分解中(3) id 计划任务分解表单中的ID
    public int updateByIdStateThree(Long companyId,String id,Long userId){
        List<String> list = getList(id);
        int res =tRiskTaskAllocationMapper.updateByIdStateThree(list);
        List<TRiskTaskAllocationList> tAll = tRiskTaskAllocationMapper.selectByMoreId(companyId,list);
        return res ;
    }

    //风险辨识后TaskAll中State修改为已辨识/评价中(4)
    public int updateByIdStateSeven(Long companyId,Integer taskAllocatId,Long userId){
        int result = tRiskTaskAllocationMapper.updateByIdStateSeven(taskAllocatId);
        if(result>0){
            //计划中风险点的辨识完成/评价中
            tRiskProcessOperationRecordMapper.updateByRiskPointIdAndRecognizeOk
                (companyId, taskAllocatId.toString());
        }
        return result;
    }

    //风险评价措施添加之后该条风险点状态要修改为 5 : 审核中/(已评价) 
    public int updateByIdStateNine(Long companyId,Integer taskAllocatId,Long userId){
        int result = tRiskTaskAllocationMapper.updateByIdStateNine(taskAllocatId);
        if(result>0){
            //计划中风险点的评价完成/审核中
            tRiskProcessOperationRecordMapper.updateByRiskPointIdAndAppraiseOk
                (companyId,taskAllocatId.toString());
        }
        return result;
    }

    //评价审核-审核-审核-对象评价审核该条风险点状态要修改为已审核(10)
    public int updateByIdStateEleven(Long companyId,Integer id,String allid,Long userId){
        int result = tRiskTaskAllocationMapper.updateByIdStateEleven(id,allid);
        if(result>0){
            //计划中风险点的部门审核完成
            tRiskProcessOperationRecordMapper.updateByRiskPointIdAndAuditOk
                (companyId, allid);
        }
        return result;
    }

    //
    public int updateByIdAndProposal(String allid,String proposal){
        return tRiskTaskAllocationMapper.updateByIdAndProposal(allid,proposal);
    }

}
